package com.hudongwx.my_shian.fragment.communityFragment;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;

import com.hudongwx.my_shian.R;
import com.hudongwx.my_shian.adapter.communityAdapter.recommendAdapter.MyEssenceAdapter;
import com.hudongwx.my_shian.adapter.communityAdapter.recommendAdapter.MySunAdapter;
import com.hudongwx.my_shian.utils.API;
import com.hudongwx.my_shian.utils.MyJsonObject;

import org.json.JSONArray;
import org.json.JSONException;
import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

/**
 * A simple {@link Fragment} subclass.
 */
public class Recommend_1_Fragment extends Fragment {
    private ListView listView;
    private MyRecommendAdapter adapter;
    private JSONArray essence;
    private JSONArray sun;
    View view;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        if (null==view){
             view = inflater.inflate(R.layout.fragment_recommend_1, container, false);
            //找出控件
            listView = (ListView) view.findViewById(R.id.recommend_list);
            //适配器
            adapter = new MyRecommendAdapter(essence,sun);
            //绑定适配器到listView
            listView.setAdapter(adapter);

            essence();
            sun();
        }


        return view;
    }

    /**
     * 请求数据的方法
     */
    public void essence(){
        RequestParams params = new RequestParams(API.getCommunityEssenceUrl());
        x.http().get(params, new Callback.CommonCallback<MyJsonObject>() {
            @Override
            public void onSuccess(MyJsonObject result) {
                //请求成功
                try {
                    JSONArray data = result.getJSONArray("Data");
//                    Log.i("info1", "onSuccess: 111111111111111:"+data.length());
                    adapter.updateEssence(data);
                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                //请求错误
            }

            @Override
            public void onCancelled(CancelledException cex) {
                //请求失败
            }

            @Override
            public void onFinished() {
                //请求完
            }
        });
    }
    public void sun(){
        RequestParams params = new RequestParams(API.getCommunitySunUrl());
        x.http().get(params, new Callback.CommonCallback<MyJsonObject>() {
            @Override
            public void onSuccess(MyJsonObject result) {
                //请求成功
                try {
                    JSONArray data = result.getJSONArray("Data");
                    Log.i("info1", "onSuccess: 1111111111222222:"+data.length());
                    adapter.updateSun(data);
                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                //请求错误
            }

            @Override
            public void onCancelled(CancelledException cex) {
                //请求失败
            }

            @Override
            public void onFinished() {
                //请求完
            }
        });
    }

    //适配器
    class MyRecommendAdapter extends BaseAdapter{
        JSONArray essence;
        JSONArray sun;

        public MyRecommendAdapter(JSONArray essence,JSONArray sun) {
            this.essence = essence;
            this.sun = sun;
        }

        public void updateEssence(JSONArray essence){
            this.essence=essence;
            notifyDataSetChanged();
        }
        public void updateSun(JSONArray sun){
            this.sun=sun;
            notifyDataSetChanged();
        }

        @Override
        public int getCount() {
            return 2;
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {

            switch (position){
                case 0:
                    //解析布局
                    convertView = LayoutInflater.from(getActivity()).inflate(R.layout.recommend_essence,null);
                    //获取控件
                    ListView lvRecommend = (ListView) convertView.findViewById(R.id.recommend_essence_list);
                    if (null != essence){
                        //显示
                        lvRecommend.setVisibility(View.VISIBLE);
                        //适配器
                        MyEssenceAdapter adapter1 = new MyEssenceAdapter(essence,getActivity());
                        lvRecommend.setAdapter(adapter1);
                    }else {
                        //隐藏
                        lvRecommend.setVisibility(View.GONE);
                    }
                    break;
                case 1:
                    //解析布局
                    convertView = LayoutInflater.from(getActivity()).inflate(R.layout.recommend_sun,null);
                    ListView lvSun = (ListView) convertView.findViewById(R.id.recommend_sun_list);
                    if (null != sun){
                        //显示
                        lvSun.setVisibility(View.VISIBLE);
                        //适配器
                        MySunAdapter adapter2 = new MySunAdapter(sun,getActivity());
                        lvSun.setAdapter(adapter2);
                    }else {
                        //隐藏
                        lvSun.setVisibility(View.GONE);
                    }
                    break;
            }
            return convertView;
        }
    }

}
